package org.elanfox.intellicommunity.msg.server.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.elanfox.intellicommunity.common.constant.RedisConstant;
import org.elanfox.intellicommunity.msg.dto.CmdDTO;
import org.elanfox.intellicommunity.msg.dto.RawSetResultDTO;
import org.elanfox.intellicommunity.msg.ienum.CmdEnums;
import org.elanfox.intellicommunity.msg.server.mq.MsgProvider;
import org.elanfox.intellicommunity.msg.server.service.CmdSetReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/3/18
 */
@Service
@Slf4j
public class CmdSetReaderImpl implements CmdSetReader {
    @Autowired
    private MsgProvider msgProvider;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public void lfScope(String hexStr) {
//        RawSetResultDTO setResultDTO = new RawSetResultDTO(CmdEnums.Code.LF_SCOPE, hexStr);
//
//        //得到当前指令
//        CmdDTO cmdDTO = (CmdDTO) redisTemplate.opsForHash()
//                .get(RedisConstant.KEY_CMD + setResultDTO.getDeviceAddr(), CmdEnums.Code.LF_SCOPE.getCode());
//        if (cmdDTO == null) {
//            throw new NullPointerException("当前没有该指令" + CmdEnums.Code.LF_SCOPE.getDesc());
//        }
//        cmdDTO.setRawMsgDTO(setResultDTO);
//        redisTemplate.opsForHash().put(
//                RedisConstant.getKeyCmd(cmdDTO.getHexAddr()),
//                CmdEnums.Code.LF_SCOPE.getCode(),
//                cmdDTO
//        );
//
//        //通知应用端执行业务
//        msgProvider.sendCommandResp(cmdDTO);
    }

    @Override
    public void rssiFilter(String hexStr) {
//        RawMsgDTO msgDTO = new RawSetResultDTO(CmdEnums.Code.RSSI_FILTER, hexStr);
//        CmdDTO cmdDTO = (CmdDTO) redisTemplate.opsForHash()
//                .get(RedisConstant.KEY_CMD + msgDTO.getDeviceAddr(), CmdEnums.Code.RSSI_FILTER.getCode());
//        if (cmdDTO == null) {
//            throw new NullPointerException("当前没有该指令" + CmdEnums.Code.RSSI_FILTER.getDesc());
//        }
//
//        cmdDTO.setRawMsgDTO(msgDTO);
//        redisTemplate.opsForHash().put(
//                RedisConstant.getKeyCmd(cmdDTO.getHexAddr()),
//                CmdEnums.Code.RSSI_FILTER.getCode(),
//                cmdDTO
//        );
//
//        //通知应用端执行业务
//        msgProvider.sendCommandResp(cmdDTO);
    }

    @Override
    public void buzzerStatus(String hexStr) {

    }

    @Override
    public void deviceAddr(String hexStr) {

    }

    @Override
    public void filterTime(String hexStr) {

    }

    @Override
    public void rename(String hexStr) {

    }

    @Override
    public void netCommunication(String hexStr) {

    }

    @Override
    public void net(String hexStr) {

    }

    @Override
    public void read(CmdEnums.Code cmdCode, String hexStr) {
        RawSetResultDTO setResultDTO = new RawSetResultDTO(CmdEnums.Code.LF_SCOPE, hexStr);
        //得到当前指令
        CmdDTO cmdDTO = (CmdDTO) redisTemplate.opsForHash()
                .get(RedisConstant.getKeyCmd(setResultDTO.getRawHexAddr()), cmdCode);
        if (cmdDTO == null) {
            throw new NullPointerException("当前没有该指令" + CmdEnums.Code.LF_SCOPE.getDesc());
        }
        cmdDTO.setRawMsgDTO(setResultDTO);
        redisTemplate.opsForHash().put(
                RedisConstant.getKeyCmd(cmdDTO.getHexAddr()),
                cmdCode,
                cmdDTO
        );

        //通知应用端执行业务
        msgProvider.sendCommandResp(cmdDTO);
    }
}
